<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Article extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'article';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'open_time_text',
        'end_time_text',
        'share_image_str',
        'images_array',
    ];
    public function getShareImageStrAttr($value, $data)
    {
        return $data['share_image'] ? cdnurl($data['share_image'], true) : '';
    }
    public function getImagesArrayAttr($value, $data)
    {
        if ($data['images']) {
            $arr = explode(',', $data['images']);

            foreach ($arr as &$v) {
                $v = cdnurl($v, true);
            }
            return $arr;
        }
        return [];
    }
    public function getOpentimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['opentime']) ? $data['opentime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function setEndtimeAttr($value, $data)
    {
        return strtotime($value);
    }
    public function setOpentimeAttr($value, $data)
    {
        return strtotime($value);
    }


    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('过期')];
    }

    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function admin()
    {
        return $this->belongsTo('Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
